package com.ruoyi.web.controller.xxc;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.news.domain.XcxGoods;
import com.ruoyi.news.domain.XcxNews;
import com.ruoyi.news.service.IXcxGoodsService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 小程序-商品主Controller
 * 
 * @author ruoyi
 * @date 2024-12-21
 */
@RestController
@RequestMapping("/xcx/goods")
public class XcxGoodsController extends BaseController
{
    @Autowired
    private IXcxGoodsService xcxGoodsService;

    /**
     * 查询小程序-商品主列表
     */
    //@PreAuthorize("@ss.hasPermi('system:goods:list')")
    @GetMapping("/list")
    public TableDataInfo list(XcxGoods xcxGoods)
    {
        startPage();
        List<XcxGoods> list = xcxGoodsService.selectXcxGoodsList(xcxGoods);
        return getDataTable(list);
    }

    @GetMapping(value = "/readNum/{id}")
    public AjaxResult readNum(@PathVariable("id") String id)
    {
        XcxGoods xcxGoods = xcxGoodsService.selectXcxGoodsById(id);
        if(xcxGoods.getViewUserCount() == null){
            xcxGoods.setViewUserCount(1);

        }else {
            Integer read = Integer.parseInt(xcxGoods.getViewUserCount().toString()) + 1;
            xcxGoods.setViewUserCount( read );
        }
        int flag =  xcxGoodsService.updateXcxGoods(xcxGoods);
        if(flag >0)
            return success(xcxGoods);
        else
            return error("系统异常");
    }

    /**
     * 导出小程序-商品主列表
     */
    @PreAuthorize("@ss.hasPermi('system:goods:export')")
    @Log(title = "小程序-商品主", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, XcxGoods xcxGoods)
    {
        List<XcxGoods> list = xcxGoodsService.selectXcxGoodsList(xcxGoods);
        ExcelUtil<XcxGoods> util = new ExcelUtil<XcxGoods>(XcxGoods.class);
        util.exportExcel(response, list, "小程序-商品主数据");
    }

    /**
     * 获取小程序-商品主详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:goods:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(xcxGoodsService.selectXcxGoodsById(id));
    }

    /**
     * 新增小程序-商品主
     */
    @PreAuthorize("@ss.hasPermi('system:goods:add')")
    @Log(title = "小程序-商品主", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody XcxGoods xcxGoods, @AuthenticationPrincipal UserDetails user)
    {
        LoginUser loginUser =  (LoginUser)user;
        SysUser sysUser = loginUser.getUser();
        xcxGoods.setCreateBy(sysUser.getUserId()+"");
        return toAjax(xcxGoodsService.insertXcxGoods(xcxGoods));
    }

    /**
     * 修改小程序-商品主
     */
    @PreAuthorize("@ss.hasPermi('system:goods:edit')")
    @Log(title = "小程序-商品主", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody XcxGoods xcxGoods)
    {
        return toAjax(xcxGoodsService.updateXcxGoods(xcxGoods));
    }

    /**
     * 删除小程序-商品主
     */
    @PreAuthorize("@ss.hasPermi('system:goods:remove')")
    @Log(title = "小程序-商品主", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(xcxGoodsService.deleteXcxGoodsByIds(ids));
    }
}
